package mof.gov.dmdc.view.exportadapter;

import java.io.IOException;

import java.text.SimpleDateFormat;

import java.util.Date;

import javax.faces.event.ActionEvent;
import javax.faces.event.ValueChangeEvent;

import mof.gov.dmdc.view.common.Common;
import mof.gov.dmdc.view.common.ExportUtils;

import oracle.adf.model.binding.DCIteratorBinding;

import oracle.adf.view.rich.component.rich.RichDialog;

import oracle.jbo.AttributeDef;
import oracle.jbo.NavigatableRowIterator;
import oracle.jbo.RowSetIterator;

import oracle.jbo.ViewObject;

public class DmdcExportAdapter {
    public DmdcExportAdapter() {
        super();
    }
    private String encode = "unicode";
    private String exportType = "all";
    private String fileType = "excel";
    private long startIndex = 1;
    private long endIndex = 2;
    private boolean ischange = false;
    private String[] exportlist;

    public void doExport(ActionEvent actionEvent) throws IOException {
        DCIteratorBinding dCIter = ExportUtils.getSearchIteratorBinding();
        NavigatableRowIterator naviga = dCIter.getNavigatableRowIterator();

        int originalRangerStart = dCIter.getRangeStart();
        int originalRangerSize = dCIter.getRangeSize();
        int originalIndex = naviga.getCurrentRowIndex();
        long maxRowCount = dCIter.getEstimatedRowCount();
        String currentRowKey = dCIter.getCurrentRowKeyString();
        
        RowSetIterator rowSetIter = dCIter.getRowSetIterator();
        System.out.println( "Row Count:" + rowSetIter.getRowCount());
        System.out.println( "Row Count:" + rowSetIter.getFetchedRowCount());
        System.out.println("Row...." + rowSetIter.getEstimatedRangePageCount());
        System.out.println("DC Iterator:" + dCIter.getEstimatedRowCount());
        System.out.println("Dc iterater ranger zise:" + dCIter.getRangeSize());
        System.out.println("Dc iterater ranger zise:" + dCIter.getRangeStart());
        ViewObject view = dCIter.getViewObject();
        System.out.println("adflkasdflskadf" + view.getEstimatedRowCount());
        


        if ("all".equalsIgnoreCase(exportType)) {
            naviga.setRangeStart(0);
            naviga.first();
            rowSetIter.setRangeStart(0);
            rowSetIter.first();
        } else {
            if ("page".equalsIgnoreCase(exportType)) {
                maxRowCount = 20;
                naviga.scrollRange(0);
                naviga.setCurrentRowAtRangeIndex(0);
            } else {
                maxRowCount = endIndex - startIndex + 1;
                naviga.scrollRange((int)startIndex - 1);
                naviga.setCurrentRowAtRangeIndex(0);
            }
        }
        RowSetIterator iter = (RowSetIterator)naviga;
        String[] attrs = getEnableExpColumn();
        AttributeDef[] attrDefs = dCIter.getAttributeDefs(attrs);
        boolean isTCVN = encode.equals("unicode") ? false : true;
        Date dateNow = new Date();
        SimpleDateFormat formatDate = new SimpleDateFormat("ddMMyyy-hhmmss");
        /*-----String fileName = formatDate.format(dateNow**********/
        String fileName = ExportUtils.getTableName();

        if ("excel".equalsIgnoreCase(fileType)) {
            fileName += ".xls";
            ExportUtils.createXLS(rowSetIter, attrDefs, attrs, maxRowCount, fileName, isTCVN);
        } else {
            if ("text".equalsIgnoreCase(fileType)) {
                fileName += ".txt";
                ExportUtils.createTXT(rowSetIter, attrDefs, attrs, maxRowCount, fileName, isTCVN);
            } else {
                fileName += ".xml";
                ExportUtils.createXML(rowSetIter, attrs, maxRowCount, fileName, isTCVN);
            }
        }

        dCIter.setRangeStart(originalRangerStart);
        dCIter.setRangeSize(originalRangerSize);
        //dCIter.setRangeSize(65000);
        dCIter.setCurrentRowIndexInRange(originalIndex);
        dCIter.setCurrentRowWithKey(currentRowKey);
        actionEvent.getComponent(); //Cha de lam gi!
    }

    private String[] getEnableExpColumn() {
        ViewObject vo = ExportUtils.getExportDefIteratorBinding().getViewObject();
        String[] strings;
        if (ischange) {
            strings = new String[(int)exportlist.length];
            strings = exportlist;
        } else {
            strings = new String[(int)vo.getEstimatedRowCount()];
            int idx = 0;
            do {
                strings[idx] =
                        vo.hasNext() ? vo.next().getAttribute("ColName").toString() : vo.first().getAttribute("ColName").toString();
                idx++;
            } while (vo.hasNext() && idx < vo.getEstimatedRowCount());
        }
        return strings;
    }

    public void setEncode(String encode) {
        this.encode = encode;
    }

    public String getEncode() {
        return encode;
    }

    public void setExportType(String exportType) {
        this.exportType = exportType;
    }

    public String getExportType() {
        return exportType;
    }

    public void setFileType(String fileType) {
        this.fileType = fileType;
    }

    public String getFileType() {
        return fileType;
    }

    public void setStartIndex(long startIndex) {
        this.startIndex = startIndex;
    }

    public long getStartIndex() {
        return startIndex;
    }

    public void setEndIndex(long endIndex) {
        this.endIndex = endIndex;
    }

    public long getEndIndex() {
        return endIndex;
    }

    public void exportTypeValueChange(ValueChangeEvent valueChangeEvent) {
        Common.refreshComponent(valueChangeEvent.getComponent().getParent());
    }

    public void setIschange(boolean ischange) {
        this.ischange = ischange;
    }

    public boolean isIschange() {
        return ischange;
    }

    public void setExportlist(String[] exportlist) {
        this.exportlist = exportlist;
    }

    public String[] getExportlist() {
        return exportlist;
    }

}
